<?php
// $Id: shop.php 359 2012-05-08 08:41:19Z tomqin@gmail.com $

defined('IN_ADMIN') or exit('Access Denied!');

class Shop_Action extends Action_Admin{

    public function init(){
        parent::init();
        $this->shop_id = intval($this->params);

        $this->shop = App::module('table_shop', 'table', array($this->user));
        if(!in_array($this->method, array('list','cache')) && !$this->shop->check_user_right($this->shop_id)){
            $this->renderDialog('您无权管理此商铺信息！');
        }
    }

    public function onModify(){
        $this->setVar('show_validator', 1);
        $data = $this->shop->get_data($this->shop_id);
        if($data){
            $this->setVar('data', $data);
            $this->render('admin/shop/edit');
        }else{
            $this->renderDialog('商铺信息不存在，无法读取信息！');
        }
    }

    public function onCreate(){
        $this->setVar('show_validator', 1);
        if($this->user->status != USER_ADMIN){
            $this->renderDialog('无权创建商铺！');
        }else{
            $this->render('admin/shop/edit');
        }
    }

    public function onList(){
        $perpage = 20;
        $page = isset($_GET['p'])?intval($_GET['p']):1;
        $search = $order = $_GET;
        $list = $this->shop->search($search, $order, $page, $total, $perpage);
        $pages = pages($total, $page, $perpage, '?p={PAGE}&'.http_build_query($search));

        $this->setVar('search', $search);
        $this->setVar('order', $order);
        $this->setVar('list', $list);
        $this->setVar('pages', $pages);
        $this->render('admin/shop/list');
    }

    public function onHome(){
        $shop = $this->shop->get_data($this->shop_id, true);
        $this->setVar('shop', $shop);
        $this->render('admin/shop/home');
    }

    public function onSave(){
        if(array_get_bool($_POST, 'dosubmit')){
            $method  = array_get_string($_POST, 'method');
            switch($method){
                case 'modify':
                    $this->shop->update($this->shop_id, $_POST);
                    $this->renderDialog('修改成功！', '/admin/shop/modify/'.$this->shop_id);
                break;
                case 'create':
                    $_POST['city_id'] = App::getCityId();
                    $this->shop->insert($_POST);
                    $shop_type = array_get_int($_POST, 'shop_type');
                    $this->renderDialog('添加成功！', '/admin/shop/list?shop_type='.$shop_type);
                break;
            }
        }else{
            $this->renderDialog('无效的参数！');
        }
    }

    public function onCache(){
        $perpage = 50;
        $page    = isset($_GET['p'])?intval($_GET['p']):0;
        $search  = $order = $_GET;
        $list    = $this->shop->search($search, $order, $page, $total, $perpage);
        $allpage = ceil($total / $perpage);

        foreach($list as $item){
            $this->shop->update_list_data($item['shop_id'], $item['shop_type']);
        }
        $items = $perpage * $page;
        if($items >= $total)
            $items = $total;

        if($page < $allpage){
            $this->renderDialog("批量缓存处理（{$items}/{$total}）完毕！", '/admin/shop/cache?p='.($page+1).'&'.http_build_query($search));
        }else{
            $this->renderDialog("批量缓存处理（{$items}/{$total}）完毕！");
        }
    }

    public function onSortlist(){
        if(array_get_bool($_POST, 'dosubmit')){
            $list = $this->shop->sort(array_get($_POST, 'sorts'));
        }
        $list = $this->onList();
    }

    function __destruct(){}
}
?>